-- CALC_BOSS_BOX_BONUS

-- 计算BOSS宝箱的奖励
-- @param user       玩家对象
-- @param dungeonId  地牢id
-- @param element    地牢元素
-- @param layer      所在层
return function(user, dungeonId, element, layer)
    -- 获取配置的参数
    local args      = DungeonM.queryElement(element, "dbase");
    local randSeed  = DungeonM.getRandSeed("DUNGEON_BOSS_BOX");
    local randSeed2 = DungeonM.getRandSeed("DUNGEON_BOSS_BOX");
    local amount    = args["amount"] or 1;
    local classId, elemId, bossId, rank;
    local arr, fetchArr;

    if dungeonId == nil then
        dungeonId = user.dbase:query("currentDungeon");
    end

    -- 如果是没有配置boss的小关卡
    if DungeonAreaM.query(dungeonId, "type") == "child" then
        dungeonId = DungeonAreaM.query(dungeonId, "parent_id");
    end

    if args["is_artifact_chips"] == 1 or args["is_artifact_relic"] == 1 then
        -- 神器碎片
        elemId  = DungeonBossM.query(dungeonId, "boss_elem");
        bossId  = DungeonM.queryElement(elemId, "dbase")["class_id"];
        arr     = MonsterM.query(bossId, "artifact_chips");
        classId = FormulaM.invoke("FETCH_BY_RAND", arr, randSeed);

        -- 神器碎片数量10，遗物数量2
        if ItemM.query(classId, "type") == ITEM_TYPE_ARTIFACT_SCRAP then
            amount = 10;
        else
            amount = 2;
        end

        return { 1, classId, amount, };
    elseif args["is_specific_honor"] == 1 then
        -- 冈布奥の罐
        return { 1, 1511, 1, };
    elseif args["is_food"] == 1 then
        -- 食材，随机:2阶食材20个，3阶食材10个，4阶特定食材5个;4阶食材有保底
        local boss_box_food_times = user.dbase:query("boss_box_food_times", 0);
        if CombatStatM.getProgress(4) == 0 then
            boss_box_food_times = 0;
        end
        local ratio = 332 - 166 * boss_box_food_times;

        fetchArr = { {["rank"] = 2, ["amount"] = 20, ["ratio"] = ratio, },
                     {["rank"] = 3, ["amount"] = 10, ["ratio"] = ratio, },
                     {["rank"] = 4, ["amount"] = 5,  ["ratio"] = 1000 - 2 * ratio, }, };

        local foodInfo = fetchElemBySeed(fetchArr, randSeed);
        rank = foodInfo["rank"];
        amount = foodInfo["amount"];

        if rank == 4 then
            boss_box_food_times = 0;
            elemId  = DungeonBossM.query(dungeonId, "boss_elem");
            bossId  = DungeonM.queryElement(elemId, "dbase")["class_id"];
            arr     = MonsterM.query(bossId, "food_material");
            classId = FormulaM.invoke("FETCH_BY_RAND", arr, randSeed2);
        else
            boss_box_food_times = boss_box_food_times + 1;
            arr = ItemM.getFoodMaterial(rank);
            classId = FormulaM.invoke("FETCH_BY_RAND", arr, randSeed2);
        end
        user.dbase:set("boss_box_food_times", boss_box_food_times);
        return { 1, classId, amount, };
    elseif args["is_honor"] == 1 or args["is_slime"] == 1 then
        -- 冈布奥碎片
        -- 取配置
        local slimeInfo = DungeonAreaM.query(dungeonId, "slime");
        local slimeArr  = table.values(slimeInfo);

        if #slimeArr > 0 then
            classId = slimeArr[1];
        elseif dungeonId == 1 then
            -- 第1个迷宫没有冈布奥产出，需要特殊处理
            classId = 1
        else
            -- 取不到是配置有问题
            cclog(string.format("dungeon_id 为 %d的取不到冈布奥配置", dungeonId));
            classId = 1;
        end

        if args["is_honor"] == 1 then
            -- 冈布奥碎片
            return { 1, PetM.query(classId, "chips"), amount, };
        else
            -- 冈布奥
            return { 3, classId, 1, };
        end
    elseif args["is_fruit"] == 1 then
        -- 世界树果实
        return { 1, 1110, amount, };
    elseif args["is_money"] == 1 then
        -- 金币
        return { 2, "money", amount, };
    elseif args["is_scroll"] == 1 then
        -- 随机rank阶卷轴
        fetchArr = ItemM.getScrollByRank(args["rank"]);
        classId  = FormulaM.invoke("FETCH_BY_RAND", fetchArr, randSeed2);

        return { 1, classId, amount, };
    elseif args["is_scroll_bag"] == 1 then
        -- 卷轴包，给一票卷轴
        classId = 7280;
        return { 1, classId, amount, };
    elseif args["is_equip"] == 1 then
        if type(args["rank"]) == "number" then
            rank = args["rank"];
        else
            fetchArr = args["rank"];
            rank = FormulaM.invoke("FETCH_BY_RAND", fetchArr, randSeed);
        end
        -- 是宝物，随机rank星一件
        fetchArr = ItemM.getEquipByRank(rank);
        classId = FormulaM.invoke("FETCH_BY_RAND", fetchArr, randSeed2);
        return { 1, classId, amount, };
    elseif args["is_specific_equip"] == 1 then
        -- 特殊规则宝物，30及40层随机获得5~6阶装备，50层开始必定获得6阶装备
        rank = 6;
        if layer < 50 then
            fetchArr = { 5, 6, };
            rank = FormulaM.invoke("FETCH_BY_RAND", fetchArr, randSeed);
        end
        fetchArr = ItemM.getEquipByRank(rank);
        classId = FormulaM.invoke("FETCH_BY_RAND", fetchArr, randSeed2);
        return { 1, classId, amount, };
    elseif args["is_longzhu"] == 1 then
        -- 龙珠
        return { 1, 1500, amount, };
    elseif args["is_search_point"] == 1 then
        -- 探索点
        amount = amount + layer * 5;
        return { 2, "search_point", amount, };
    elseif args["is_hp"] == 1 then
        -- 超级血瓶，传入量+楼层*2
        return { 2, "limit_hp", amount + layer * args["fix_co"], };
    elseif args["is_mp"] == 1 then
        -- 超级蓝瓶，传入量+楼层*2
        return { 2, "limit_mp", amount + layer * args["fix_co"], };
    elseif args["is_apprentice"] == 1 then
        -- 冈布奥学徒
        return { 2, "idle_worker_num", amount, };
    elseif args["is_hallow_stone"] == 1 then
        -- 圣者之石
        return { 1, 1506, amount, };
    elseif args["is_trump"] == 1 then
        -- 秘宝
        return { 1, 1999, amount, };
    elseif args["best_lamp_oil"] then
        -- 圣灯油
        return { 1, 7179, amount, };
    elseif args["bonus"] then
        -- 直接使用配置的
        return args["bonus"];
    else
        -- 保护
        return { 2, "money", 1, };
    end
end
